package com.crb.crm.manage.service.biz.impl;

import com.crb.crm.manage.utils.DateUtil;
import com.crb.crm.manage.dao.biz.SystemConfigInfoMapper;
import com.crb.crm.manage.dao.biz.SystemConfigMapper;
import com.crb.crm.manage.entity.biz.SystemConfigDTO;
import com.crb.crm.manage.entity.biz.SystemConfigSearchDTO;
import com.crb.crm.manage.entity.biz.SystemConfigVO;
import com.crb.crm.manage.pojo.biz.SystemConfig;
import com.crb.crm.manage.service.biz.SystemConfigService;
import com.crb.crm.manage.utils.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {
	private static final Logger logger = LoggerFactory.getLogger(SystemConfigServiceImpl.class);
	@Autowired
	private SystemConfigMapper systemConfigMapper;
	@Autowired
	private SystemConfigInfoMapper systemConfigInfoMapper;

	@Override
	public PageDataResult getSystemConfigs(SystemConfigSearchDTO systemConfigSearch, int page, int limit) {
		// 时间处理
		if (null != systemConfigSearch) {
			if (StringUtils.isNotEmpty(systemConfigSearch.getQinsertTimeStart())
					&& StringUtils.isEmpty(systemConfigSearch.getQinsertTimeEnd())) {
				systemConfigSearch.setQinsertTimeEnd(DateUtil.format(new Date()));
			} else if (StringUtils.isEmpty(systemConfigSearch.getQinsertTimeStart())
					&& StringUtils.isNotEmpty(systemConfigSearch.getQinsertTimeEnd())) {
				systemConfigSearch.setQinsertTimeStart(DateUtil.format(new Date()));
			}
			if (StringUtils.isNotEmpty(systemConfigSearch.getQinsertTimeStart())
					&& StringUtils.isNotEmpty(systemConfigSearch.getQinsertTimeEnd())) {
				if (systemConfigSearch.getQinsertTimeEnd().compareTo(systemConfigSearch.getQinsertTimeStart()) < 0) {
					String temp = systemConfigSearch.getQinsertTimeStart();
					systemConfigSearch.setQinsertTimeStart(systemConfigSearch.getQinsertTimeEnd());
					systemConfigSearch.setQinsertTimeEnd(temp);
				}
			}
		}
		PageDataResult pdr = new PageDataResult();
		PageHelper.startPage(page, limit);
		List<SystemConfigDTO> urList = systemConfigInfoMapper.getSystemConfig(systemConfigSearch);
		// 获取分页查询后的数据
		PageInfo<SystemConfigDTO> pageInfo = new PageInfo<>(urList);
		// 设置获取到的总记录数total：
		pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
		pdr.setList(urList);
		return pdr;
	}

	@Override
	public String setSystemConfig(SystemConfig systemConfig) {
		SystemConfig systemConfigDb = systemConfigMapper.selectByPrimaryKey(systemConfig.getId());
		if (systemConfigDb == null) {
			systemConfig.setInsertTime(new Date());
			systemConfigMapper.insertSelective(systemConfig);
		} else {
			systemConfig.setUpdateTime(new Date());
			systemConfigMapper.updateByPrimaryKeySelective(systemConfig);
		}
		return "ok";
	}

	@Override
	public String setDelSystemConfig(Integer id, Integer isDel) {
		systemConfigInfoMapper.setDelSystemConfig(id, isDel);
		return "ok";
	}

	@Override
	public SystemConfigVO getSystemConfig(Integer id) {
		SystemConfig systemConfig = systemConfigMapper.selectByPrimaryKey(id);
		if (systemConfig != null) {
			SystemConfigVO vo = new SystemConfigVO();
			BeanUtils.copyProperties(systemConfig, vo);
			return vo;
		}
		return null;
	}

}
